package com.factory.generic;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class ClassUtils {

    public static List<Class> getAllClassByInterface(Class c) {

        //返回结果
        List<Class> returnClassList = new ArrayList<>();


//
        //如果不是一接口，不做处理
        if (c.isInterface()) {
            try {
                //获得当前的包名
                String packageName = c.getPackage().getName();

                System.out.println(packageName);
                getClasses(packageName);
            } catch (IOException e) {
                e.printStackTrace();
            }


        }

        return null;


    }


    /**
     * 从包中查找出所有的类，在jar包中的不查找
     *
     * @param packageName
     * @return
     */
    private static List<Class> getClasses(String packageName) throws IOException {
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        String path = packageName.replace(".", "/");
        System.out.println(path);

        //获取path路径下的目录，包括path目录本身
        Enumeration<URL> resources = contextClassLoader.getResources(path);
        List<File> dirs = new ArrayList<>();


        while (resources.hasMoreElements()) {
            URL element = resources.nextElement();
            String fileStr = element.getFile();
            System.out.println(fileStr);
            dirs.add(new File(fileStr));

        }


        return null;

    }


    public static void main(String[] args) {


        getAllClassByInterface(IProduct.class);

    }

}
